package com.zdkj.common.core.exceltools.impl;


import com.alibaba.fastjson.JSON;
import com.zdkj.common.core.exceltools.ColInfo;
import com.zdkj.common.core.exceltools.ExcelUtil;
import com.zdkj.common.core.exceltools.ExcelVersion;
import com.zdkj.common.core.exceltools.IExcelExport;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: zdkj
 * @Package: com.zdkj.subway.st.exceltools.impl
 * @ClassName: CommonExcelExport
 * @Description:
 * @Author: kangjian
 * @CreateDate: 2020/1/13 10:01
 * @Version: 1.0
 */
@Slf4j
@Service
public class CommonExcelExport implements IExcelExport {
    @Override
    public <T> Workbook parse(List<T> obj) {
        return   this.parse(obj, ExcelVersion.XLSX);
    }

    @Override
    public <T> Workbook parse(List<T> obj, ExcelVersion excelVersion) {

        Workbook wb=null;
        //缓存
        if(obj==null||obj.size()==0)
        {
            throw new RuntimeException("导出数据为空，请核实导出信息！");
        }
        try {
            List<ColInfo> headertocolumn = ExcelUtil.getBeanColbyOder(obj.get(0));
            List<String> columnName = new ArrayList<String>();
            List<String> displayName = new ArrayList<String>();
            for(ColInfo colInfo:headertocolumn)
            {
                columnName.add(colInfo.getColname());
                displayName.add(colInfo.getDisplayname());
            }
            wb= ExcelUtil.createWorkbook(excelVersion);
            Sheet sheet;
            Row row;
            Cell cell;
            sheet = wb.createSheet("表数据列表");
            sheet.setDefaultColumnWidth(30);
            row = sheet.createRow(0);
            CellStyle headerStyle = ExcelUtil.getHeaderCellStyle(wb);
            CellStyle cellStyleStyle = ExcelUtil.getCellStyle(wb);
            for (int i = 0; i < displayName.size(); i++) {
                    cell = row.createCell(i);
                    cell.setCellValue(displayName.get(i));
                    cell.setCellStyle(headerStyle);
            }

            int k=0;
            for (int r = 0; r < obj.size(); r++) {
                T ot=obj.get(r);
                row = sheet.createRow(r + 1);
                Map<String, Object> cellData = (Map<String, Object>) JSON.toJSON(ot);
                for (int j = 0; j < columnName.size(); j++) {
                    cell = row.createCell(j);
                    if (cellData.get(columnName.get(j)) == null || "".equals(cellData.get(columnName.get(j)))) {
                        cell.setCellValue("");
                    } else {
                        String colname=columnName.get(j);
                        Object value=cellData.get(colname);
                        Field name = ot.getClass().getDeclaredField(colname);
                        name.setAccessible(true);
                        String type=name.getGenericType().toString();
                        switch (type) {
                            case "char":
                            case "java.lang.Character":
                            case "class java.lang.String":
                                cell.setCellValue((String) value);
                                break;
                            case "int":

                            case "class java.lang.Integer":
                                cell.setCellValue((Integer) value);
                                break;
                            case "long":
                            case "class java.lang.Long":
                                cell.setCellValue((Long) value);
                                break;
                            case "float":
                                cell.setCellValue((Double) value);
                                break;
                            case "double":
                                cell.setCellValue((Double) value);
                                break;
                            case "class java.lang.Double":
                                cell.setCellValue((Double) value);
                                break;
                            case "class java.lang.Float":
                                cell.setCellValue((Double) value);
                                break;
                            case "class java.lang.Short":
                                cell.setCellValue((Double) value);
                                break;
                            case "class java.math.BigDecimal":
                                cell.setCellValue(new Double(value.toString()));
                                break;
                            case "class java.time.LocalDateTime":
                                cell.setCellValue(value.toString());
                                break;
                            default:
                                break;
                        }
                        cell.setCellStyle(cellStyleStyle);
                    }
                }
            }
        } catch (Exception e) {
            log.error("",e);
        }

        return wb;
    }
}
